package com.badger.spring.cloud.alibaba.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.badger.spring.cloud.alibaba.consumer.api.ProviderFeignApi;
import com.badger.spring.cloud.alibaba.consumer.api.dto.SeataProviderDto;
import com.badger.spring.cloud.alibaba.consumer.mapper.SeataConsumerMapper;
import com.badger.spring.cloud.alibaba.consumer.model.SeataConsumer;

import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/consumer")
public class SeataConsumerController {

    @Autowired
    SeataConsumerMapper seataConsumerMapper;
    @Autowired
    ProviderFeignApi providerFeignApi;

    @ApiOperation(value = "一阶段回滚")
    @PostMapping("/one/save")
    @GlobalTransactional
    public String saveOne(String name) {
        log.info("seata分布式事务Id:{}", RootContext.getXID());
        SeataConsumer entity = new SeataConsumer();
        entity.setName(name);
        seataConsumerMapper.insert(entity);
        int aa = 100 / 0;
        return "ok";
    }

    @ApiOperation(value = "全局成功")
    @PostMapping("/save")
    @GlobalTransactional
    public synchronized String saveEntity(String name) {
        log.info("seata分布式事务Id:{}", RootContext.getXID());
        SeataConsumer entity = new SeataConsumer();
        entity.setName(name);
        seataConsumerMapper.insert(entity);
        SeataProviderDto dto = new SeataProviderDto();
        dto.setConsumerId(entity.getId());
        dto.setNum(100);
        providerFeignApi.saveEntity(dto);
        return "ok";
    }

    @ApiOperation(value = "主任务结束失败")
    @PostMapping("/ex/main")
    @Transactional
    @GlobalTransactional
    public String ex(String name) {
        log.info("seata分布式事务Id:{}", RootContext.getXID());
        SeataConsumer entity = new SeataConsumer();
        entity.setName(name);
        seataConsumerMapper.insert(entity);
        SeataProviderDto dto = new SeataProviderDto();
        dto.setConsumerId(entity.getId());
        dto.setNum(100);
        providerFeignApi.saveEntity(dto);
        int aa = 100 / 0;
        return "ok";
    }

    @ApiOperation(value = "二阶段回滚")
    @PostMapping("/save/reback")
    @GlobalTransactional
    public String saveEntity1(String name) {
        log.info("seata分布式事务Id:{}", RootContext.getXID());
        SeataConsumer entity = new SeataConsumer();
        entity.setName(name);
        seataConsumerMapper.insert(entity);
        SeataProviderDto dto = new SeataProviderDto();
        dto.setConsumerId(entity.getId());
        dto.setNum(1000);
        providerFeignApi.saveEntity(dto);
        providerFeignApi.saveEx(dto);
        return "ok";
    }

    @ApiOperation(value = "远程调用超时")
    @PostMapping("/save/timeout")
    @GlobalTransactional
    public String saveEntity2(String name) {
        SeataConsumer entity = new SeataConsumer();
        entity.setName(name);
        seataConsumerMapper.insert(entity);
        SeataProviderDto dto = new SeataProviderDto();
        dto.setConsumerId(entity.getId());
        dto.setNum(1000);
        providerFeignApi.saveEntityTimeOut(dto);
        System.out.println(name);
        return "ok";
    }
}
